$(document).ready(function() {

    $('.new_password').on('input propertychange', function () {
        var text = $('.new_password').val();
        var len = text.length;
        var strongthTip = $('.strong_tip');
        var sec = 0;
        var regxs = new Array();
        regxs[0] = /[^a-zA-Z0-9_]/g;
        regxs[1] = /[a-z]/g;
        regxs[2] = /[0-9]/g;

        if (len >= 6) {
            for (var i = 0; i < regxs.length; i++) {
                if (text.match(regxs[i])) {
                    sec++;
                }
            }
        }
        if (sec == 0) {
            strongthTip.text('不能小于6个字符');
            $('.password-strong').css('color', '#f39c12')
        }
        else if (sec == 1) {
            strongthTip.text('弱');
            $('.password-strong').css('color', '#f39c12')
        }
        else if (sec == 2) {
            strongthTip.text('中');
            $('.password-strong').css('color', 'orange')
        }
        else if (sec == 3) {
            strongthTip.text('强');
            $('.password-strong').css('color', 'green')
        }
        else if (sec == 4) {
            strongthTip.text('超强');
            $('.password-strong').css('color', 'green')
        }


    });
    // var passOff = true;

    $('.ensure_password').focusout(function () {
        var val1 = $('.new_password').val();
        var val2 = $(this).val();
        if (val1 != val2) {
            // passOff = false;
            layer.msg('两次输入密码不一致，请重新填写');
            return;
        }
    });

////////////////
    $('.change_password').click(function () {
        layer.open({
            type: 2,
            title: false,
            anim: 1,
            closeBtn: 1,
            area: ['400px', '300px'],
            id: 'LAY_change',
            shadeClose: true,
            shade: 0.6,
            btnAlign: 'c',
            moveType: 1,
            content: 'http://114.116.72.220:9010/src/change_password.html'
        });
    });
    var userRightName = $.cookie('UserRightText');
    var userRight = $.cookie('UserRight');
    console.log(userRight)
    if (userRight == null) {
        window.location.href = "http://114.116.72.220:9010/src/login.html";
    }
//判断权限
    var Title = document.title;
    var page_kind = 0;
    if (Title.indexOf('订单') != -1) {
        page_kind = 1;
    } else if (Title.indexOf('商品') != -1) {
        page_kind = 2;
    } else if (Title.indexOf('账号') != -1) {
        page_kind = 4;
    }else if (Title.indexOf('密码') != -1) {
        page_kind = 3;
    }

    if (page_kind != 3) {
        if (userRight == 4 && page_kind != 1) {
            layer.msg('您只有查看订单模块的权力', {time: 500}, function () {
                window.location.href = "http://114.116.72.220:9010/src/hire_order.html";
            })

        }

        if (userRight == 3 && page_kind != 2) {
            layer.msg('您只有查看商品模块的权力', {time: 500}, function () {
                window.location.href = "http://114.116.72.220:9010/src/product_list.html";
            })
        }

        if (userRight == 2 && page_kind == 4) {
            layer.msg('您没有账号管理的权力', {time: 500}, function () {
                window.location.href = "http://114.116.72.220:9010/src/product_list.html";
            })
        }
    }


    $('.manger-name').text(userRightName);
    $('.clear_date').click(function () {
        $('#reservation').val('请选择时间');
    });

    $('.save_btn').click(function () {
        var passOff = true;
        if ($('.new_password').val() == '' || $('.ensure_password').val() == '') {
            passOff = false;
            layer.msg('内容填写不完整');
            return;
        }
        if ($('.new_password').val() != $('.ensure_password').val()) {
            passOff = false;
            layer.msg('两次输入密码不一致，请重新填写');
            return;
        }

        if (passOff) {
            $.ajax({
                type: "get",
                url: "http://114.116.72.220:9999/UpdatePassword",
                async: true,
                data: {
                    userId: $.cookie('UserId'),
                    oldPwd: $('.old_password').val(),
                    newPwd: $('.new_password').val()
                },
                success: function (data) {
                    if (data[0] == 'success') {
                        layer.msg('修改成功', {time: 500}, function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            $.cookie("UserId", null);
                            $.cookie("UserRight", null);
                            $.cookie("UserRightText", null);
                            window.parent.location.reload();
                        });

                    } else {
                        layer.msg('修改失败，原始密码不正确', {time: 500}, function () {});
                    }
                }
            });
        }
    });

    $(".exit_login").click(function () {
        $.cookie("UserId", null);
        $.cookie("UserRight", null);
        $.cookie("UserRightText", null);
        window.location.href = "http://114.116.72.220:9010/src/login.html";
    });

});

